package com.example.summary.lianbiao;

/**
 * Created by wzn on 2019/3/29.
 */
public class LianBIaoHeBing {

    public static void main(String[] args) {
        LianBiaoNode a = new LianBiaoNode(1, new LianBiaoNode(3, new LianBiaoNode(5, new LianBiaoNode(7, null))));
        LianBiaoNode b = new LianBiaoNode(2, new LianBiaoNode(4, new LianBiaoNode(6, new LianBiaoNode(8, new LianBiaoNode(10, new LianBiaoNode(12, null))))));

        LianBiaoNode merge = merge(a, b);

        while (null != merge) {
            System.out.println(merge.getI());
            merge = merge.getNext();
        }
    }

    private static LianBiaoNode merge(LianBiaoNode a, LianBiaoNode b) {

        LianBiaoNode x = null;

        if (null == a) {
            return b;
        }

        if (null == b) {
            return a;
        }

        if (a.getI() < b.getI()) {
            x = a;
            x.setNext(merge(a.getNext(), b));
        } else {
            x = b;
            x.setNext(merge(a, b.getNext()));
        }

        return x;
    }
}
